//
//  MyMovieViewController.m
//  UI16_DouBan
//
//  Created by apple on 15/11/4.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "MyMovieViewController.h"
#import "MyMovieTableViewCell.h"
#import "DataBaseCollect.h"
#import "Movie.h"
#import "MovieDeteliViewController.h"
@interface MyMovieViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain)UITableView *myTableV;

@property (nonatomic, retain)NSMutableArray *allArray;
@end

@implementation MyMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"我的电影";
    
    self.myTableV = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableV];
    self.myTableV.delegate = self;
    self.myTableV.dataSource = self;
    [_myTableV release];
    
    //去掉多余cell
    self.myTableV.tableFooterView = [UIView new];
    //去掉cell分割线
    self.myTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //注册
    [self.myTableV registerClass:[MyMovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.allArray = [NSMutableArray array];
    [[DataBaseCollect shareDataBase] openDB];
    [[DataBaseCollect shareDataBase] createDB];
    NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"isLogin"];
    self.allArray = [[DataBaseCollect shareDataBase] selectMovieArray:name];
    NSLog(@"----------%@",self.allArray);

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    static NSString *cellIdentifier = @"cell";
    MyMovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    Movie *movie = [self.allArray objectAtIndex:indexPath.row];
    cell.movie = movie;
    
//    cell.textLabel.text = @"gg";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 选中时有置灰效果, 离开时消失选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MovieDeteliViewController *movieDVC = [[MovieDeteliViewController alloc]init];
    Movie *movie = [self.allArray objectAtIndex:indexPath.row];
    movieDVC.movieID = movie.movieId;
    
    [self.navigationController pushViewController:movieDVC animated:YES];
    
    [movieDVC release];
}


//删除编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //设置tableView编辑状态,随按钮状态改变而改变
    [self.myTableV setEditing:editing animated:editing];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//设置删除文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"白白";
}
// 向左拖拽 删除 "白白" 逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Movie *movie = [self.allArray objectAtIndex:indexPath.row];
    NSString *name = movie.movieName;
    NSLog(@"************name = %@", name);
    NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
    //登录时,取出当前的用户名是多少
    NSString *userName = [loginUserDef stringForKey:@"name"];
    
    //根据电影名 删除数据库里面的数据
    [[DataBaseCollect shareDataBase]deleteMovie:movie.movieName user:userName];
    
    //删除电影名(self.allArray)  中的 数据
    [self.allArray removeObjectAtIndex:indexPath.row];
    
    //删除cell上面的数据
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
    [self.myTableV reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
